# -*- coding: utf-8 -*-# filename: handle.py
import web

import receive
import dealwithReplyMsg as deal
import reply


class Handle(object):
    toUser = ''
    fromUser = ''
    content = ''

    def POST(self):
        try:
            webData = web.data()
            print "Handle Post webdata is ", webData
            # 后台打日志
            recMsg = receive.parse_xml(webData)
            if isinstance(recMsg, receive.Msg):
                toUser = recMsg.FromUserName
                fromUser = recMsg.ToUserName
                if recMsg.MsgType == 'text':
                    msg = receive.parse_xml(webData)
                    content = deal.process(msg.Content)
                    replyMsg = reply.TextMsg(toUser, fromUser, content)
                    return replyMsg.send()
                elif recMsg.MsgType == 'image':
                    image = receive.parse_xml(webData)
                    replyMsg = reply.ImageMsg(toUser, fromUser, image.MediaId)
                    return replyMsg.send()
            else:
                print "暂且不处理"
                return "success"
        except Exception, Argment:
            content = '错误'
            print 'exception'
        replyMsg = reply.TextMsg(toUser, fromUser, content)
        return replyMsg.send()
